package com.ljkj.cordial.chat.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.lanjiu.lib.imclient.ims.IMSUtils;
import com.lanjiu.lib.utils.CollectionUtils;
import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.adapter.SearchListAdapter;
import com.ljkj.cordial.chat.base.BaseActivity;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.dao.notify.FriendTable;
import com.ljkj.cordial.chat.dao.notify.GroupTable;
import com.ljkj.cordial.chat.util.ObserverThread;
import com.nic.view.widget.ContainsEmojiEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @description:
 * @author: Panwei
 * @date: 2020/2/15 21:24
 */
public class SearchLocalActivity extends BaseActivity {

    @BindView(R.id.et_search)
    ContainsEmojiEditText etSearch;
    @BindView(R.id.recycler_ui)
    RecyclerView mRecyclerView;
    @BindView(R.id.no_result)
    TextView mNoResultView;

    private SearchListAdapter mAdapter;

    @Override
    public int getContentLayoutId() {
        return R.layout.activity_search_local;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        getToolbar().setTitle("查找");
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setHasFixedSize(true);
        mAdapter = new SearchListAdapter(this);
        mAdapter.setOnItemClickListener((view, item, position) -> {
            if (item.itemType == SearchListAdapter.ITEM_FRIEND) {
                if (item.relation == SearchListAdapter.RELATION_FRIENDLY) {
                    String userType = IMSUtils.userTypeAnalysis(item.id);
                    FriendInfoActivity.jumpTo(this, IMSContacts.UserType.REGISTERED_USER.equals(userType) ? Canstants.KEY_REGISTER : Canstants.KEY_VISITOR, item.name, item.id, userType);
                } else if (item.relation == SearchListAdapter.RELATION_SELF) {
                    startActivity(new Intent(this, PersonalActivity.class));
                }
            } else {
                if (item.relation == SearchListAdapter.RELATION_FRIENDLY) {
                    GroupChatSetActivity.jumpToSend(this, item.name, item.id, IMSUtils.userTypeAnalysis(item.id));
                }
            }
        });
        mRecyclerView.setAdapter(mAdapter);
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s)) {
                    mAdapter.clear();
                    mNoResultView.setVisibility(View.GONE);
                } else {
                    search(s.toString());
                }
            }
        });
    }

    private void search(final String mSearchKeyword) {
        ObserverThread.create().onNextThread2(new ObserverThread.OnListActionListener<SearchListAdapter.ItemBean>() {
            @Override
            public void onAction(List<SearchListAdapter.ItemBean> list) {
                if (!CollectionUtils.isEmpty(list)) {
                    mNoResultView.setVisibility(View.GONE);
                    mAdapter.setDataSet(list);
                } else {
                    mNoResultView.setVisibility(View.VISIBLE);
                    mAdapter.clear();
                }
            }

            @Override
            public List<SearchListAdapter.ItemBean> onThreadAction() {
                List<SearchListAdapter.ItemBean> itemList = new ArrayList<>();

                List<FriendTable> f = FriendTable.search(mSearchKeyword);
                for (FriendTable table : f)
                    itemList.add(new SearchListAdapter.ItemBean(table));

                List<GroupTable> g = GroupTable.search(mSearchKeyword);
                for (GroupTable table : g)
                    itemList.add(new SearchListAdapter.ItemBean(table));

//                List<GroupMemberTable> gm = GroupMemberTable.search(mSearchKeyword);
//                for (GroupMemberTable table : gm)
//                    itemList.add(new SearchListAdapter.ItemBean(table, mSearchKeyword));

                return itemList;
            }
        });
    }
}
